package com.letian.web.controller.record;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.PhoneBook;
import com.letian.record.service.IPhoneBookService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 电话本Controller
 * 
 * @author bmz
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/phone/book")
public class PhoneBookController extends BaseController
{
    @Autowired
    private IPhoneBookService phoneBookService;

    /**
     * 查询电话本列表
     */
    @PreAuthorize("@ss.hasPermi('phone:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(PhoneBook phoneBook)
    {
        startPage();
        List<PhoneBook> list = phoneBookService.selectPhoneBookList(phoneBook);
        return getDataTable(list);
    }

    /**
     * 导出电话本列表
     */
    @PreAuthorize("@ss.hasPermi('phone:book:export')")
    @Log(title = "电话本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PhoneBook phoneBook)
    {
        List<PhoneBook> list = phoneBookService.selectPhoneBookList(phoneBook);
        ExcelUtil<PhoneBook> util = new ExcelUtil<PhoneBook>(PhoneBook.class);
        util.exportExcel(response, list, "电话本数据");
    }

    /**
     * 获取电话本详细信息
     */
    @PreAuthorize("@ss.hasPermi('phone:book:query')")
    @GetMapping(value = "/{phoneId}")
    public AjaxResult getInfo(@PathVariable("phoneId") Long phoneId)
    {
        return success(phoneBookService.selectPhoneBookByPhoneId(phoneId));
    }

    /**
     * 新增电话本
     */
    @PreAuthorize("@ss.hasPermi('phone:book:add')")
    @Log(title = "电话本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PhoneBook phoneBook)
    {
        phoneBook.setCreateBy(getUsername());
        return toAjax(phoneBookService.insertPhoneBook(phoneBook));
    }

    /**
     * 修改电话本
     */
    @PreAuthorize("@ss.hasPermi('phone:book:edit')")
    @Log(title = "电话本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PhoneBook phoneBook)
    {
        phoneBook.setUpdateBy(getUsername());
        Long count = phoneBook.getUpdateCount() == null ? 1 : phoneBook.getUpdateCount() + 1;
        phoneBook.setUpdateCount(count);
        return toAjax(phoneBookService.updatePhoneBook(phoneBook));
    }

    /**
     * 删除电话本
     */
    @PreAuthorize("@ss.hasPermi('phone:book:remove')")
    @Log(title = "电话本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{phoneIds}")
    public AjaxResult remove(@PathVariable Long[] phoneIds)
    {
        return toAjax(phoneBookService.deletePhoneBookByPhoneIds(phoneIds));
    }
}
